from django.shortcuts import render

from AskDoctor.ser import (
    AskDoctorClassificationSerializer,
    AskDoctorDepartmentSerializer,
    DoctorPatientSerializer,
    PatientProfileSerializer,
    PatientReviewSerializer,
    DoctorSerializer
)

from AskDoctor.models import (
    AskDoctorClassificationModel,
    AskDoctorDepartmentModel,
    DoctorPatientModel,
    PatientProfile,
    PatientReviewModel,
    DoctorModel
)


from rest_framework.generics import ListAPIView
from rest_framework.mixins import ListModelMixin, RetrieveModelMixin
from rest_framework.permissions import IsAuthenticatedOrReadOnly
from rest_framework.pagination import PageNumberPagination
from rest_framework.filters import OrderingFilter
from rest_framework.response import Response

# 分页配置
class StandardResultsSetPagination(PageNumberPagination):
    page_size = 10
    page_size_query_param = 'page_size'
    max_page_size = 100

# 排序配置
class CustomOrderingFilter(OrderingFilter):
    ordering_params = ('name', 'rating', 'price', 'doctor_name', 'birth_date', 'review_date')

# 医生 ListAPIView
class DoctorListView(ListAPIView):
    filter_backends = [CustomOrderingFilter]
    ordering_fields = ['rating', 'price']
    pagination_class = StandardResultsSetPagination

    def get_queryset(self):
        return DoctorModel.objects.all()

    def list(self, request, *args, **kwargs):
        queryset = self.filter_queryset(self.get_queryset())
        page = self.paginate_queryset(queryset)
        if page is not None:
            serializer = DoctorSerializer(page, many=True)
            return self.get_paginated_response(serializer.data)
        serializer = DoctorSerializer(queryset, many=True)
        return Response({
            'message': serializer.data,
            'code': 200
        })

# 患者简介 ListAPIView
class PatientProfileListView(ListAPIView):
    filter_backends = [CustomOrderingFilter]
    ordering_fields = ['birth_date', 'name']
    pagination_class = StandardResultsSetPagination

    def get_queryset(self):
        return PatientProfile.objects.all()

    def list(self, request, *args, **kwargs):
        queryset = self.filter_queryset(self.get_queryset())
        page = self.paginate_queryset(queryset)
        if page is not None:
            serializer = PatientProfileSerializer(page, many=True)
            return self.get_paginated_response(serializer.data)
        serializer = PatientProfileSerializer(queryset, many=True)
        return Response({
            'message': serializer.data,
            'code': 200
        })

# 医患问答 ListAPIView
class DoctorPatientListView(ListAPIView):
    filter_backends = [CustomOrderingFilter]
    ordering_fields = ['date']
    pagination_class = StandardResultsSetPagination

    def get_queryset(self):
        return DoctorPatientModel.objects.all()

    def list(self, request, *args, **kwargs):
        queryset = self.filter_queryset(self.get_queryset())
        page = self.paginate_queryset(queryset)
        if page is not None:
            serializer = DoctorPatientSerializer(page, many=True)
            return self.get_paginated_response(serializer.data)
        serializer = DoctorPatientSerializer(queryset, many=True)
        return Response({
            'message': serializer.data,
            'code': 200
        })

# 患者评价 ListAPIView
class PatientReviewListView(ListAPIView):
    filter_backends = [CustomOrderingFilter]
    ordering_fields = ['review_date', 'rating']
    pagination_class = StandardResultsSetPagination

    def get_queryset(self):
        return PatientReviewModel.objects.all()

    def list(self, request, *args, **kwargs):
        queryset = self.filter_queryset(self.get_queryset())
        page = self.paginate_queryset(queryset)
        if page is not None:
            serializer = PatientReviewSerializer(page, many=True)
            return self.get_paginated_response(serializer.data)
        serializer = PatientReviewSerializer(queryset, many=True)
        return Response({
            'message': serializer.data,
            'code': 200
        })

# 问医生 ListAPIView
class AskDoctorClassificationListView(ListAPIView):
    filter_backends = [CustomOrderingFilter]
    ordering_fields = ['classification']
    pagination_class = StandardResultsSetPagination

    def get_queryset(self):
        return AskDoctorClassificationModel.objects.all()

    def list(self, request, *args, **kwargs):
        queryset = self.filter_queryset(self.get_queryset())
        page = self.paginate_queryset(queryset)
        if page is not None:
            serializer = AskDoctorClassificationSerializer(page, many=True)
            return self.get_paginated_response(serializer.data)
        serializer = AskDoctorClassificationSerializer(queryset, many=True)
        return Response({
            'message': serializer.data,
            'code': 200
        })

# 问医生科室 ListAPIView
class AskDoctorDepartmentListView(ListAPIView):
    filter_backends = [CustomOrderingFilter]
    ordering_fields = ['askDoctor_department']
    pagination_class = StandardResultsSetPagination

    def get_queryset(self):
        return AskDoctorDepartmentModel.objects.all()

    def list(self, request, *args, **kwargs):
        queryset = self.filter_queryset(self.get_queryset())
        page = self.paginate_queryset(queryset)
        if page is not None:
            serializer = AskDoctorDepartmentSerializer(page, many=True)
            return self.get_paginated_response(serializer.data)
        serializer = AskDoctorDepartmentSerializer(queryset, many=True)
        return Response({
            'message': serializer.data,
            'code': 200
        })